<script setup>
import { ref } from 'vue'
import http from '@/utils/http'
import { useRouter } from 'vue-router'
import { useUserStore } from '@/stores/userStore'

const userStore = useUserStore()
const username = ref('')
const password = ref('')
const router = useRouter()

const goToRegister = () => {
  router.push('/register')
}

const handleLogin = async () => {
  try {
    const res = await http.post('auth/login_or_register/', {
      user: username.value,
      password: password.value
    })
    if (res.status === 200) {
      // 将用户信息存入 localStorage
      localStorage.setItem('user', JSON.stringify(res.data));
    }

    console.log("res", res)
    // 保存用户信息到 Pinia
    userStore.setUser({
      user_id: res.data.user_id,     // 注意这里也要传 user_id
      username: res.data.username,
      avatar: res.data.avatar,
      role: res.data.role
    })
    console.log("userStore.name", userStore.name)

    // 跳转到不同页面
    const role = res.data.role
    console.log(role)
    if (role === 'admin') {
      router.push('/admin')
    } else if (role === 'staff') {
      router.push('/staff-chat')
    } else {
      router.push('/')
      console.log("userStore.id:", userStore.id)
    }
  } catch (err) {
    alert(err.response?.data?.error || '登录失败')
  }
}
</script>

<template>
  <div class="login-container">
    <div class="login-card">
      <h2>欢迎登录</h2>
      <form @submit.prevent="handleLogin">
        <input v-model="username" link placeholder="用户名" required />
        <input v-model="password" type="password" placeholder="密码" required />
        <button type="submit">登录</button>
      </form>
      <div class="register-link">
        <div>还没有账号？</div>
        <button type="button" @click="goToRegister">去注册</button>
      </div>

    </div>
  </div>
</template>


<style scoped>
.login-container {
  background: url('/bg.jpg') center/cover no-repeat;
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
}

.login-card {
  background: rgba(255, 255, 255, 0.15); /* 半透明 */
  border-radius: 20px;
  box-shadow: 0 8px 32px 0 rgba(0, 0, 0, 0.2);
  backdrop-filter: blur(12px); /* 玻璃模糊效果 */
  -webkit-backdrop-filter: blur(12px); /* 兼容 Safari */
  border: 1px solid rgba(255, 255, 255, 0.2);
  padding: 48px 36px;
  max-width: 400px;
  width: 90%;
  text-align: center;
  font-family: 'Segoe UI', sans-serif;
  transition: all 0.3s ease-in-out;
}

.login-card h2 {
  color: #ffffff;
  font-size: 28px;
  font-weight: 700;
  margin-bottom: 32px;
  letter-spacing: 1px;
}

.login-card input {
  width: 100%;
  padding: 14px 16px;
  margin-bottom: 18px;
  border: none;
  border-radius: 10px;
  background-color: rgba(255, 255, 255, 0.2);
  color: #fff;
  font-size: 15px;
  outline: none;
  transition: background 0.3s ease;
}

.login-card input::placeholder {
  color: rgba(255, 255, 255, 0.7);
}

.login-card input:focus {
  background-color: rgba(255, 255, 255, 0.3);
}

.login-card button {
  width: 100%;
  padding: 12px;
  background: linear-gradient(to right, #f9d423, #ff4e50);
  border: none;
  border-radius: 30px;
  color: #fff;
  font-size: 16px;
  font-weight: bold;
  cursor: pointer;
  transition: transform 0.2s ease, box-shadow 0.2s ease;
}

.login-card button:hover {
  transform: scale(1.03);
  box-shadow: 0 4px 15px rgba(255, 78, 80, 0.4);
}

.register-link {
  margin-top: 16px;
  font-size: 14px;
  color: rgba(255, 255, 255, 0.8);
  display: flex;
  justify-content: space-around;
}

.register-link button {
  width: 80px;
  background: none;
  border: none;
  color: #ffe082;
  cursor: pointer;
  font-weight: bold;
  margin-left: 4px;
  padding: 0;
  font-size: 14px;
  text-decoration: underline;
}

.register-link button:hover {
  opacity: 0.8;
}
</style>